//
//  UILabelAdditions.m
//  MClient
//
//  Created by  史正烨 on 10-11-16.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UILabelAdditions.h"

@implementation UILabel(MCLabelSetting)

// 动画显示，之后消失
- (void)showToast
{
	UILabel* toastMsg = (UILabel*)[self viewWithTag:2222];
	MClientAppDelegate *appDelegate = (MClientAppDelegate*)[[UIApplication sharedApplication] delegate];
	UINavigationController* navController = [appDelegate navController];
	
	BOOL isPortrait = UIInterfaceOrientationIsPortrait(navController.interfaceOrientation);
	
	CGFloat width = isPortrait ? navController.view.frame.size.width : navController.view.frame.size.height;
	CGFloat height=	isPortrait ? navController.view.frame.size.height : navController.view.frame.size.width;
	
	CGSize size = [toastMsg.text sizeWithFont:[UIFont systemFontOfSize:18]
					  constrainedToSize:CGSizeMake(width-80, height-80)
						  lineBreakMode:UILineBreakModeWordWrap];
	
	CGRect frame = CGRectMake((width-size.width-20)/2, (height-size.height-50)/2, size.width+20, size.height+10);
	UIImageView* toastView = [[UIImageView alloc] initWithFrame:frame];
	toastView.image = [[UIImage imageNamed:@"bkToastMsg.png"] stretchableImageWithLeftCapWidth:18 topCapHeight:20];
	//toastView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
	toastView.tag = 3333;
	UILabel* toastLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, size.width, size.height)];
	toastLabel.text = self.toastMessage;
	toastLabel.numberOfLines = 99;
	toastLabel.textColor = [UIColor whiteColor];
	toastLabel.backgroundColor = [UIColor clearColor];
	[toastView addSubview:toastLabel];[toastLabel release];
	[navController.view addSubview:toastView];
	[navController.view bringSubviewToFront:toastView];
	[toastView release];
	
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
	[UIView setAnimationDuration:0.5];
	//[UIView setAnimationDelegate:self];
	//[UIView setAnimationDidStopSelector:@selector(toastMessageStop)];
	toastView.alpha = 1.0;
	[UIView commitAnimations];
	
	[NSTimer scheduledTimerWithTimeInterval:1
									 target:self 
								   selector:@selector(toastMessageStop) 
								   userInfo:nil
									repeats:NO];
}

- (void)toastMessageStop {
	MClientAppDelegate *appDelegate = (MClientAppDelegate*)[[UIApplication sharedApplication] delegate];
	UINavigationController* navController = [appDelegate navController];
	
	UIView* toastView = (UILabel*)[navController.view viewWithTag:3333];
	[toastView removeFromSuperview];
}

- (NSString*) toastMessage {
	UIView*  toastIcon = [self viewWithTag:1111];
	UILabel* toastMsg = (UILabel*)[self viewWithTag:2222];
	if (toastIcon && toastMsg) {
		return [NSString stringWithFormat:@"%@", toastMsg.text];
	} else {
		return @"";
	}
}

- (void) setToastMessage:(NSString*)message {
	UIImageView*  toastIcon = (UIImageView*)[self viewWithTag:1111];
	if (!toastIcon) {
		toastIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"headerArrow.png"]];
		toastIcon.tag = 1111;
		[self addSubview:toastIcon];
		[toastIcon release];
		
		UILabel* btLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 25, 9)];
		btLabel.text = @"必填";
		btLabel.textColor = [UIColor grayColor];
		btLabel.font = [UIFont systemFontOfSize:8];
		[toastIcon addSubview:btLabel];
		[btLabel release];
	}
	UILabel* toastMsg = (UILabel*)[self viewWithTag:2222];
	if (toastMsg) {
		toastMsg.text = [NSString stringWithFormat:@"%@", message];
	} else {
		toastMsg = [[UILabel alloc] initWithFrame:CGRectZero];
		toastMsg.text = [NSString stringWithFormat:@"%@", message];
		toastMsg.tag = 2222;
		[self addSubview:toastMsg];
		[toastMsg release];
	}
//	self.exclusiveTouch = YES;
//	self.userInteractionEnabled = YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UIView*  toastIcon = [self viewWithTag:1111];
	UILabel* toastMsg = (UILabel*)[self viewWithTag:2222];
	if (toastIcon && toastMsg) {
		[self showToast];
	}
}

@end

@implementation MCLabel
- (UITextField*)input {
	return input;
}
- (void)setInput:(UITextField *)textField {
	input = textField;
	self.exclusiveTouch = YES;
	self.userInteractionEnabled = YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if (self.input) {
		[self.input becomeFirstResponder];
	}
}

@end

